//
//  HomeRootViewController.swift
//  fruit-helper-swift
//
//  Created by 赵隆杰 on 2022/6/11.
//

import UIKit

class HomeRootViewController: BaseViewController {
        
    private lazy var collection = { () -> UICollectionView in
        let flowLayout = UICollectionViewFlowLayout.init()
        flowLayout.scrollDirection = .vertical
        flowLayout.minimumLineSpacing = 0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        
        let collection = UICollectionView.init(frame: view.frame, collectionViewLayout: flowLayout)
        collection.backgroundColor = .white
        collection.delegate = self
        collection.dataSource = self
        collection.register(HomeCollectionViewCell.self, forCellWithReuseIdentifier: HomeCollectionViewCell.className())
        return collection
    }()
    
    private lazy var datas = NSMutableArray.init()
   

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "书单"
        
        view.addSubview(self.collection)
        
        
//        navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "add"), style: .plain, target: self, action: #selector(addAction))
        
        
        self.datas = NSMutableArray(array: LocalData().getAllHomeBooks())
        self.collection.reloadData()
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
    }

}

extension HomeRootViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = self.collection.dequeueReusableCell(withReuseIdentifier: HomeCollectionViewCell.className(), for: indexPath) as! HomeCollectionViewCell
        cell.model = (self.datas[indexPath.row] as! BookModel)
        return cell
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.datas.count;
    }
    
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let width = (SL_WINDOW_WIDTH - 20) / 3.0
        let height = width + 50
        return CGSize(width: width, height: height)
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        guard let model: BookModel = (datas[indexPath.item] as? BookModel) else { return }
        
        if let typeStr = model.imageType {
            if Int(typeStr) == -1 {
                addAction()
                return
            }
        }
        
        let addVc = HomeEditViewController()
        addVc.model = model
        navigationController?.present(addVc, animated: true, completion: {
            
        })
    }
    
    @objc func addAction() {
        
        let addVc = HomeAddViewController()
        addVc.backEnve = { () -> Void in
            self.datas = NSMutableArray(array: LocalData().getAllHomeBooks())
            self.collection.reloadData()
        }
        navigationController?.present(addVc, animated: true, completion: {
            
        })
        
    }
}


extension HomeCollectionViewCell {
     
}
